﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Data.Common;
using System.Collections.Generic;
using QzEbook.Common;
using QzEbook.AccessObject;
using QzEbook.Common.Emum;
using QzEbook.Admin;

public partial class module_Sider : System.Web.UI.UserControl
{
    StringBuilder sb;
    private SiderType showType;

    public SiderType ShowType
    {
        get { return showType; }
        set { showType = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            buildSiderBar();
        }
    }

    /// <summary>
    /// 开始创建菜单项;
    /// </summary>
    private void buildSiderBar()
    {
        sb = new StringBuilder();
        sb.AppendLine("<ul>");
        switch (showType)
        {
            case SiderType.Index :
                this.indexType();
                break;
            case SiderType.Search :
                this.searchType();
                break;
            case SiderType.Book :
                this.bookType();
                break;
            case SiderType.User :
                this.userType();
                break;
            default:
                this.indexType();
                break;
        }

        
        sb.AppendLine("</ul>");
    }

    private void userType()
    {
        if (SessionManager.LoginUser != null)
            outputLi("loginpane", "用户<sup>Beta</sup>", new selectDelegate(buildLoginPane));
        outputLi("userpane", "最新用户", new selectDelegate(getUserList));
        outputLi("tagpane", "标签", new selectDelegate(getTagList));
    }

    /// <summary>
    /// 隐藏,由于远程麻烦.
    /// </summary>
    private void hideSiderBar()
    {
    }

    /// <summary>
    /// 首引项;
    /// </summary>
    private void indexType()
    {
        if(SessionManager.LoginUser != null)
            outputLi("loginpane", "用户<sup>Beta</sup>", new selectDelegate(buildLoginPane));
        
        outputLi("searchpane", "搜索<sup>Beta</sup>", new selectDelegate(buildSearcher));
        outputLi("categorypane", "分类列表", new selectDelegate(buildCategory));
        outputLi("messagepane", "最新留言", new selectDelegate(getPostMessageList));
        outputLi("userpane", "最新用户", new selectDelegate(getUserList));
        outputLi("tagpane", "标签", new selectDelegate(getTagList));
    }

    private void searchType()
    {
        outputLi("searchpane", "搜索<sup>Beta</sup>", new selectDelegate(buildSearcher));
        outputLi("messagepane", "最新留言", new selectDelegate(getPostMessageList));
    }
    private void bookType()
    {
        if (Session[SessionManager.LOGINUSER] != null)
            outputLi("loginpane", "用户<sup>Beta</sup>", new selectDelegate(buildLoginPane));
        outputLi("categorypane", "分类列表", new selectDelegate(buildCategory));
        outputLi("tagpane", "标签", new selectDelegate(getTagList));
    }


    private void outputLi(string cssClass ,string title,selectDelegate select)
    {
        sb.AppendFormat("<li id=\"{0}\">",cssClass);
        sb.AppendFormat("<h3>{0}</h3>", title);
        sb.AppendLine("<dl>");

        select();

        sb.AppendLine("</dl>");
        sb.AppendLine("</li>");
    }

    private void buildLoginPane()
    {
        if (SessionManager.LoginUser != null)
        {
            SessionUser userSession = Session[SessionManager.LOGINUSER] as SessionUser;
            sb.AppendFormat("<dt>欢迎您:{0}</dt>",userSession.Name);
            sb.AppendFormat("<dd><a href=\"{1}\" title=\"{0}\">{0}</a></dd>", "管理账户", "admin/UserPane.xhtml");
            sb.AppendFormat("<dd><a href=\"user.xhtml?Uid={1}\" title=\"{0}\">{0}</a></dd>", "我的杂志页", userSession.Uid);
            sb.AppendFormat("<dd><a href=\"admin/Login.xhtml?logout=1&raw={1}\" title=\"{0}\">{0}</a></dd>", "退出", Request.Url.PathAndQuery);
            return;
        }
        /*sb.Append("<dt><label>用户名(UserName):</label><input id=\"UName\" type=\"text\" name=\"name\" width=\"100\" /></dt>");
        sb.Append("<dt><label>密码(PassWord):   </label><input id=\"PassWord\" type=\"password\" name=\"pwd\" width=\"100\" /></dt>");
        sb.Append("<dt><button id=\"searchBtn\">登陆(Login)</button></dt>");*/
    }

    /// <summary>
    /// 写入搜索条;
    /// </summary>
    private void buildSearcher()
    {
        string searchValue = "请输入搜索条件";
        if(!String.IsNullOrEmpty(Request["Key"])){
            searchValue = Request["Key"];
        }
        sb.AppendFormat("<input id=\"searchTextBox\" type=\"text\" name=\"p\" maxlength=\"50\" size=\"12\" autocomplete=\"off\" value=\"{0}\" />",searchValue);
        sb.AppendLine("<button id=\"searchBtn\"></button>");
    }

    /// <summary>
    /// 分类索引;
    /// </summary>
    private void buildCategory()
    {
        Category category = new Category();
        List<Category> list = category.selectList();

        StringBuilder subSb = new StringBuilder();
        int countBook=0;
        foreach (Category item in list)
        {
            countBook += item.Count;
            subSb.AppendFormat("<dt>{0}</dt>", Category.getLinkHtml(item.Id,item.Count));
        }
        sb.AppendFormat("<dt><a href=\"list.xhtml\" title=\"所有电子书\">全部({0})</a></dt>",countBook);
        sb.AppendLine(subSb.ToString());
    }

    /// <summary>
    /// 标签;
    /// </summary>
    /*private void bulidTag()
    {
        Tag tag = new Tag();
        List<Tag> list = tag.selectList();
        foreach (Tag item in list)
        {
            sb.AppendFormat("<dd>{0}</dd>", Tag.getLinkHtml(item.Id));
        }
    }*/

    /// <summary>
    /// 最新注册用户10个;
    /// </summary>
    private void getUserList()
    {
        DbCommand command = DataAccess.CreateCommand();
        command.CommandText = "Select top 10 * from Qz_User order by UID desc";
        command.CommandType = CommandType.Text;
        DataTable table = DataAccess.ExcuteSelectCommand(command);

        foreach (DataRow row in table.Rows)
        {
            sb.AppendLine();
            sb.AppendFormat("<dt><a  href=\"user.xhtml?Uid={0}\" title=\"查看{1}的主页\">{1}</a></dt>",row["UID"], row["UName"]);
            
        }
        sb.Append("<dt class=\"more\"><a  href=\"user.xhtml\" title=\"more..\">更多用户...</a></dt>");
    }

    /// <summary>
    /// 最新的一些留言;
    /// </summary>
    private void getPostMessageList()
    {
        DbCommand command = DataAccess.CreateCommand();
        StringBuilder sql = new StringBuilder();
        sql.Append("Select top 10 * from");
        sql.Append(" Qz_Comment LEFT JOIN Qz_User ON Qz_Comment.UID=Qz_User.UID");
        sql.Append(" order by CID desc");


        command.CommandText =sql.ToString();
        command.CommandType = CommandType.Text;
        DataTable table = DataAccess.ExcuteSelectCommand(command);

        foreach (DataRow row in table.Rows)
        {
            sb.AppendLine();
            sb.AppendFormat("<dt>{0}</dt>",row["UName"]);
            sb.AppendLine();
            sb.AppendFormat("<dd><a href=\"detail.xhtml?Bid={0}#p{2}\" title=\"{1}\">{1}</a></dd>",row["BID"], row["CComment"],row["CID"]);
        }
    }

    /// <summary>
    /// 取得引用最多的100个Tag;
    /// </summary>
    private void getTagList()
    {
        DataTable tagList=new Tag().selectData(100);
        foreach (DataRow tag in tagList.Rows)
        {
            sb.AppendLine();
            sb.AppendFormat("<dd><a href=\"list.xhtml?Tid={1}\" title=\"有{2}本\">{0}</a></dd>", tag["TName"], tag["TID"], tag["TRefs"]);
        }
        sb.AppendLine("<dd class=\"more\"><a href=\"tag.xhtml\"  title=\"more....\">更多.....</a></dd>");
    }

    /// <summary>
    /// 写出数据流;
    /// </summary>
    /// <param name="writer"></param>
    protected override void Render(HtmlTextWriter writer)
    {
        writer.Write(sb.ToString());
        base.Render(writer);
    }
}
